package org.ehe.erp.controller.product;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.idempotent.annotation.RepeatSubmit;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.erp.domain.product.bo.CodingRuleBo;
import org.ehe.erp.domain.product.vo.CodingRuleVo;
import org.ehe.erp.service.product.ICodingRuleService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
/**
 * 编码规则
 * @author :zhangnn
 * @className :CodingRuleController
 * @description: TODO
 * @date 2025-09-06 15:00:22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/codingRule")
public class CodingRuleController extends BaseController {

    private final ICodingRuleService codingRuleService;

    /**
     * 查询编码规则列表
     */
    @SaCheckPermission("erp:codingRule:list")
    @GetMapping("/list")
    public R<List<CodingRuleVo>> list(CodingRuleBo bo) {
        List<CodingRuleVo> list = codingRuleService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出编码规则列表
     */
    @SaCheckPermission("erp:codingRule:export")
    @Log(title = "编码规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CodingRuleBo bo, HttpServletResponse response) {
        List<CodingRuleVo> list = codingRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "编码规则", CodingRuleVo.class, response);
    }

    /**
     * 获取编码规则详细信息
     */
    @SaCheckPermission("erp:codingRule:query")
    @GetMapping("/{id}")
    public R<CodingRuleVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(codingRuleService.queryById(id));
    }

    /**
     * 新增编码规则
     */
    @SaCheckPermission("erp:codingRule:add")
    @Log(title = "编码规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CodingRuleBo bo) {
        if (!codingRuleService.checkRuleUnique(bo)) {
            return R.fail("新增编码规则失败，该层级的规则已存在");
        }
        return toAjax(codingRuleService.insertByBo(bo));
    }

    /**
     * 修改编码规则
     */
    @SaCheckPermission("erp:codingRule:edit")
    @Log(title = "编码规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CodingRuleBo bo) {
        if (!codingRuleService.checkRuleUnique(bo)) {
            return R.fail("修改编码规则失败，该层级的规则已存在");
        }
        return toAjax(codingRuleService.updateByBo(bo));
    }

    /**
     * 删除编码规则
     */
    @SaCheckPermission("erp:codingRule:remove")
    @Log(title = "编码规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(codingRuleService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取层级选项
     */
    @SaCheckPermission("erp:codingRule:query")
    @GetMapping("/levelOptions")
    public R<List<Integer>> getLevelOptions() {
        return R.ok(codingRuleService.getLevelOptions());
    }

    /**
     * 预览编码效果
     */
    @SaCheckPermission("erp:codingRule:query")
    @PostMapping("/previewCode")
    public R<Map<String, Object>> previewCode(@RequestBody CodingRuleBo bo) {
        String preview = codingRuleService.previewCode(bo);
        Map<String, Object> result = new HashMap<>();
        result.put("preview", preview);
        result.put("description", String.format("层级%d编码示例", bo.getLevel()));
        return R.ok("预览生成成功", result);
    }

    /**
     * 检查规则唯一性
     */
    @SaCheckPermission("erp:codingRule:query")
    @PostMapping("/checkUnique")
    public R<Boolean> checkUnique(@RequestBody CodingRuleBo bo) {
        boolean unique = codingRuleService.checkRuleUnique(bo);
        if (unique) {
            return R.ok("规则可用", true);
        } else {
            return R.fail("该层级的编码规则已存在", false);
        }
    }
}
